package jp.co.jbuy.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.jbuy.common.ResourceManager;
import jp.co.jbuy.common.MessageManager;

public abstract class BaseAction {

	protected Connection conn_;

	protected Connection getConnection() {
		return conn_;
	}

	protected HttpServletRequest req_;

	protected HttpServletRequest getRequest() {
		return req_;
	}

	protected HttpServletResponse resp_;

	protected HttpServletResponse getResponse() {
		return resp_;
	}

	protected ResourceBundle resource_;

	protected ResourceBundle getResource() {
		return resource_;
	}

	protected String language_;

	public abstract void action() throws Exception;

	public void prepare(Connection conn, HttpServletRequest req,
			HttpServletResponse resp, ResourceBundle res) throws Exception {
		conn_ = conn;
		req_ = req;
		resp_ = resp;
		resource_ = res;
		setLanguage();
	}

	private void setLanguage() throws Exception {
		language_ = req_.getParameter("language");
		if (language_ == null) {
			Cookie[] cookies = req_.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if ("language".equals(cookie.getName())) {
						language_ = cookie.getValue();
						break;
					}
				}
			}
			if (language_ == null) {
				language_ = ResourceManager.getSystemDefaultLanguage();
			}
		}
		else {
			boolean isExistlanguageCookie = false;
			Cookie[] cookies = req_.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if ("language".equals(cookie.getName())) {
						cookie.setValue(language_);
						/* 值变更后 必须再用response往client送 */
						resp_.addCookie(cookie);
						isExistlanguageCookie = true;
						break;
					}
				}
			}
			if (!isExistlanguageCookie) {
				Cookie cookie = new Cookie("language", URLEncoder.encode(
						language_, "UTF-8"));
				int age = ResourceManager.getLanguageCookieAge();
				cookie.setMaxAge(age * 24 * 60 * 60);
				/* Cookie保存 */
				resp_.addCookie(cookie);
			}
		}
	}

	protected void forward(String url) throws ServletException, IOException {
		req_.setAttribute("language", language_);
		RequestDispatcher rd = req_.getRequestDispatcher(url);
		rd.forward(req_, resp_);
	}

	protected int getIntParameter(String key) throws NumberFormatException {
		try {
			return Integer.parseInt(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			throw new NumberFormatException(key);
		}
	}

	protected int getIntParameter(String key, int v) {
		try {
			return Integer.parseInt(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			return v;
		}
	}

	protected int[] getIntParameterValues(String key, int[] def)
			throws Exception {

		String[] strParamValue = req_.getParameterValues(key);

		if (strParamValue != null) {
			int[] nParamValue = new int[strParamValue.length];

			for (int i = 0; i < strParamValue.length; i++) {

				try {
					nParamValue[i] = Integer.parseInt(strParamValue[i]);
				}
				catch (NumberFormatException exc) {
					throw new Exception(key);
				}
			}

			return nParamValue;
		}
		else {

			return def;
		}
	}

	protected int[] getIntParameterValues(String key) throws Exception {

		return getIntParameterValues(key, new int[0]);
	}

	protected long getLongParameter(String key) throws NumberFormatException {
		try {
			return Long.parseLong(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			throw new NumberFormatException(key);
		}
	}

	protected long getLongParameter(String key, long v) {
		try {
			return Long.parseLong(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			return v;
		}
	}

	protected long[] getLongParameterValues(String key) throws Exception {

		String[] strParamValue = req_.getParameterValues(key);

		if (strParamValue != null) {
			long[] lParamValue = new long[strParamValue.length];

			for (int i = 0; i < strParamValue.length; i++) {

				try {
					lParamValue[i] = Long.parseLong(strParamValue[i]);
				}
				catch (NumberFormatException exc) {
					throw new Exception(key);
				}
			}

			return lParamValue;
		}
		else {

			return new long[0];
		}
	}

	protected float getFloatParameter(String key) throws Exception {
		try {
			return Float.parseFloat(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			throw new Exception(key);
		}
	}

	protected float getFloatParameter(String key, float v) {
		if (req_.getParameter(key) == null) {
			return v;
		}
		try {
			return Float.parseFloat(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			return v;
		}
	}

	protected double getDoubleParameter(String key) throws Exception {
		try {
			return Double.parseDouble(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			throw new Exception(key);
		}
	}

	protected double getDoubleParameter(String key, double v) {
		if (req_.getParameter(key) == null) {
			return v;
		}
		try {
			return Double.parseDouble(req_.getParameter(key));
		}
		catch (NumberFormatException exc) {
			return v;
		}
	}

	protected double[] getDoubleParameterValues(String key) throws Exception {

		String[] strParamValue = req_.getParameterValues(key);

		if (strParamValue != null) {
			double[] dParamValue = new double[strParamValue.length];

			for (int i = 0; i < strParamValue.length; i++) {

				try {
					dParamValue[i] = Double.parseDouble(strParamValue[i]);
				}
				catch (NumberFormatException exc) {
					throw new Exception(key);
				}
			}

			return dParamValue;
		}
		else {

			return new double[0];
		}
	}

	protected boolean getBooleanParameter(String key) {
		return Boolean.valueOf(req_.getParameter(key)).booleanValue();
	}

	protected boolean getBooleanParameter(String key, boolean v) {
		String s = req_.getParameter(key);
		if (s == null)
			return v;
		return Boolean.valueOf(s).booleanValue();
	}

	protected boolean[] getBooleanParameterValues(String key) throws Exception {

		String[] strParamValue = req_.getParameterValues(key);

		if (strParamValue != null) {
			boolean[] bParamValue = new boolean[strParamValue.length];

			for (int i = 0; i < strParamValue.length; i++) {

				try {
					bParamValue[i] = Boolean.valueOf(strParamValue[i])
							.booleanValue();
				}
				catch (NumberFormatException exc) {
					throw new Exception(key);
				}
			}

			return bParamValue;
		}
		else {

			return new boolean[0];
		}
	}

}